package com.lzgcoding.onlineshopapp.feature.main.cart.component

import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.sizeIn
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import coil3.compose.SubcomposeAsyncImage
import com.lzgcoding.onlineshopapp.room.entity.ItemEntity
import com.lzgcoding.onlineshopapp.ui.theme.LargePadding
import com.lzgcoding.onlineshopapp.ui.theme.MediumPadding
import com.lzgcoding.onlineshopapp.ui.theme.SmallPadding
import com.lzgcoding.onlineshopapp.util.formatCurrency

@Composable
fun CartList(
    items: List<Pair<ItemEntity, Int>>,
    onDecrease: (ItemEntity) -> Unit,
    onIncrease: (ItemEntity) -> Unit
) {
    LazyColumn(
        modifier = Modifier
            .fillMaxWidth()
            .fillMaxHeight(0.6f)
            .padding(horizontal = LargePadding),
        verticalArrangement = Arrangement.spacedBy(LargePadding)
    ) {
        item { Spacer(Modifier.height(MediumPadding)) }
        items(items) {
            CartItem(
                pic = it.first.picUrls.first(),
                title = it.first.title,
                price = it.first.price,
                count = it.second,
                onIncrease = { onIncrease(it.first) },
                onDecrease = { onDecrease(it.first) }
            )
        }
        item { Spacer(Modifier.height(LargePadding * 2)) }
    }
}

@Composable
private fun CartItem(
    pic: String,
    title: String,
    price: Double,
    count: Int,
    onIncrease: () -> Unit,
    onDecrease: () -> Unit
) {
    Row(
        modifier = Modifier
            .fillMaxWidth()
            .clip(RoundedCornerShape(MediumPadding))
            .background(MaterialTheme.colorScheme.surfaceContainer)
            .padding(MediumPadding),
        horizontalArrangement = Arrangement.spacedBy(MediumPadding)
    ) {
        Box(
            modifier = Modifier
                .size(100.dp)
                .aspectRatio(1f)
                .clip(RoundedCornerShape(MediumPadding))
                .background(Color(0xFFefefef)),
            contentAlignment = Alignment.Center
        ) {
            SubcomposeAsyncImage(
                model = pic,
                contentDescription = title,
                contentScale = ContentScale.Fit,
                modifier = Modifier
                    .size(80.dp)
                    .aspectRatio(1f)
                    .clip(RoundedCornerShape(MediumPadding)),
                loading = {
                    Box(
                        modifier = Modifier.fillMaxSize(),
                        contentAlignment = Alignment.Center
                    ) {
                        CircularProgressIndicator()
                    }
                }
            )
        }

        Column {
            Text(
                text = title,
                style = MaterialTheme.typography.bodyLarge,
                fontWeight = FontWeight.Bold,
                maxLines = 2,
                minLines = 2,
                overflow = TextOverflow.Ellipsis
            )
            Text(
                text = price.formatCurrency(),
                style = MaterialTheme.typography.bodyMedium,
                maxLines = 1
            )
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(top = MediumPadding),
                horizontalArrangement = Arrangement.SpaceBetween,
                verticalAlignment = Alignment.CenterVertically
            ) {
                Text(
                    text = (price * count).formatCurrency(),
                    style = MaterialTheme.typography.bodyLarge,
                    fontWeight = FontWeight.Bold,
                    maxLines = 1,
                    color = Color.Red
                )
                Row(
                    verticalAlignment = Alignment.CenterVertically,
                    horizontalArrangement = Arrangement.spacedBy(SmallPadding),
                    modifier = Modifier
                        .clip(CircleShape)
                        .background(MaterialTheme.colorScheme.background)
                        .border(1.dp, Color(0xFFe9e9e9))
                ) {
                    val buttonModifier = Modifier
                        .size(26.dp)
                    Surface(
                        modifier = buttonModifier,
                        shape = CircleShape,
                        border = BorderStroke(1.dp, Color(0xFFe9e9e9)),
                        color = MaterialTheme.colorScheme.background,
                        onClick = { onDecrease() }
                    ) {
                        Text(
                            text = "-",
                            modifier = Modifier.wrapContentSize(),
                            textAlign = TextAlign.Center,
                            style = MaterialTheme.typography.labelMedium
                        )
                    }
                    Text(
                        text = "$count", modifier = Modifier
                            .sizeIn(minWidth = 36.dp)
                            .width(36.dp)
                            .wrapContentHeight(),
                        textAlign = TextAlign.Center,
                        style = MaterialTheme.typography.labelMedium,
                        fontWeight = FontWeight.Bold
                    )
                    Surface(
                        modifier = buttonModifier,
                        shape = CircleShape,
                        border = BorderStroke(1.dp, Color(0xFFe9e9e9)),
                        color = MaterialTheme.colorScheme.background,
                        onClick = { onIncrease() }
                    ) {
                        Text(
                            text = "+",
                            modifier = Modifier.wrapContentSize(),
                            textAlign = TextAlign.Center,
                            style = MaterialTheme.typography.labelMedium
                        )
                    }
                }
            }
        }
    }
}